package com.dflc.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.dflc.annotations.CheckUser;
import com.dflc.common.CommonConstant;
import com.dflc.entity.po.TBackLoginUser;
import com.dflc.entity.rep.HttpResponseEntity;
import com.dflc.service.TBackLoginUserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 描述: 用户校验切面
 * @author: OUYANG
 * @date: 2025/7/17 上午9:35
 */
@Aspect
@Component
public class UserCheckAspect {

    @Resource
    private TBackLoginUserService tBackLoginUserService;

    @Around("@annotation(checkUser)")
    public Object checkUser(ProceedingJoinPoint joinPoint, CheckUser checkUser) throws Throwable {
        Integer id3 = StpUtil.getLoginIdAsInt();
        TBackLoginUser sysLoginUser = tBackLoginUserService.getById(id3);

        if (sysLoginUser == null) {
            return HttpResponseEntity.error(500, "查询不到当前用户的信息！请检查数据！");
        } else if (sysLoginUser.getState().intValue() == CommonConstant.StateActive.Deleted.intValue()) {
            return HttpResponseEntity.error(401, "当前用户已被删除！");
        }
        // 继续执行原方法
        return joinPoint.proceed();
    }
}
